%{
    #include <iostream>
    #include<stdio.h>
    int lines = 1,reservewords=0,identifiers=0,operators=0,delimiters=0,nums=0;       
    unsigned long long chars = 0;
    void addLine(int);
    void addChar(int);
    void addReserveWord(int);
    void addDlimiters(int);
    void addIdentifier(int);
    void addOperator(int);
    void addNum(int);
%}
digit   [0-9]
letter  [A-za-z_]

num                     {digit}+(\.{digit}+)?(E[+\-]?{digit}+)?
id                      {letter}({letter|digit})*
reserveword            "break"|"main"|"continue"|"else"|"float"|"for"|"if"|"int"|"return"|"void"|"while"
operator               "-"|"+"|"="|"=="|"*"|"/"|"<="|">="
delimiter              "("|")"|"{"|"}"|";"
line_comment            (\/\/.*\n)
char                   '[^']'
string                 \"[^\"]*\"
%%
{num}  {
    printf("<%s,常数>",yytext);
    addChar(yyleng);
    addNum(1);
}
{line_comment}  {
    printf("<%s,注释>"yytext);
    addChar(yyleng);
    addLine(1);
}
{char}  {
    printf("<%s,字符>",yytext);
    addChar(yyleng);
}
{string}    {
    printf("<%s,字符串>",yytext);
    addChar(yyleng);
}
{reserveword} {
    printf("<%s,关键字>",yytext);
    addChar(yyleng);
    addReserveWord(1);
}
{identifier}    {
    printf("<%s,标识符>",yytext);
    addChar(yyleng);
    addIdentifier(1);
}

{operator}  {
    printf("<%s,标识符>",yytext);
    addChar(yyleng);
    addOperator(1);
}

{delimiter} {
    printf("<%s,标识符>",yytext);
    addChar(yyleng);
    addDlimiters(1);
}

\n      {
    addChar(yyleng);
    addLine(1);
}

.       {
    printf("<%s,错误>",yytext);
    addChar(yyleng);
}
%%
int main(void)
{
    yylex();
    printf("字符数:%d\n",chars);
    printf("行数:%d\n",lines);
    printf("标识符数:%d\n",identifiers);
    printf("保留字数:%d\n",reservewords);
    printf("界符数:%d\n",delimiters);
    printf("常数数:%d\n",nums);
    printf("操作符数:%d\n",operators);
    return 0;
}
int yywrap()    {
    return 1;
}

void addLine(int cnt)   {
    lines += cnt;
}
void addChar(int cnt)   {
    chars += cnt;
}
void addIdentifier(int cnt)   {
    identifiers += cnt;
}
void addNum(int cnt)   {
    nums += cnt;
}
void addReserveWord(int cnt)   {
    reservewords += cnt;
}
void addDlimiters(int cnt)   {
    delimiters += cnt;
}
void addChar(int cnt)   {
    chars += cnt;
}
